/*
 * JFrmPesqProduto.java
 *
 * Created on 10 de Agosto de 2008, 21:22
 */
package view;

import controller.CtrCadastroProduto;
import controller.CtrCadastroVenda;
import dao.ProdutoDAO;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import model.Produto;
import model.Venda;
import utils.ExcecaoSCEV;

/**
 *
 * @author  Administrador
 */
public class JFrmPesqVenda extends javax.swing.JPanel {

    int operacao;
    CtrCadastroProduto ctrCadastroProduto = CtrCadastroProduto.getInstance();
    Produto produto;
    List<Produto> produtos;
    CtrCadastroVenda ctrCadastroVenda = CtrCadastroVenda.getInstance();
    Venda venda;
    List<Venda> vendas;
    private TableColumn cColuna = null;
    private javax.swing.JFrame parent;

    /** Creates new form ProdutoJPanel */
    public JFrmPesqVenda(javax.swing.JFrame parent) {
        try {
            initComponents();
            this.parent = parent;
            produtos = ctrCadastroProduto.listarTodos(jComboBox.getSelectedIndex());
            atualizarTabela(produtos);

        } catch (ExcecaoSCEV ex) {
            JOptionPane.showMessageDialog(null, ex.getMsg(), "Validação", JOptionPane.ERROR_MESSAGE);
        }


        try {
            initComponents();
            this.parent = parent;
            vendas = ctrCadastroVenda.listarTodos(jComboBox.getSelectedIndex());
            atualizarTabelaVenda(vendas);
        } catch (ExcecaoSCEV ex) {
            JOptionPane.showMessageDialog(null, ex.getMsg(), "Validação", JOptionPane.ERROR_MESSAGE);
        }

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        jPanel3 = new javax.swing.JPanel();
        jButtonOk = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        jScrollPane = new javax.swing.JScrollPane();
        jTableProduto = new javax.swing.JTable();
        jPanel = new javax.swing.JPanel();
        jLabel = new javax.swing.JLabel();
        jComboBox = new javax.swing.JComboBox();
        jButtonPesquisa = new javax.swing.JButton();
        jTextFieldPesquisa = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jButtonConsultar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableVenda = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabelTotal = new javax.swing.JLabel();
        jTextFieldTotal = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jButtonCaixaVenda = new javax.swing.JButton();
        jButtonFechar = new javax.swing.JButton();
        jButtonNovo = new javax.swing.JButton();
        jButtonExcluir = new javax.swing.JButton();

        jButtonOk.setText("OK");

        jButtonCancelar.setText("Cancelar");

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(558, Short.MAX_VALUE)
                .add(jButtonOk, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 74, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButtonCancelar)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonCancelar)
                    .add(jButtonOk))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                formKeyTyped(evt);
            }
        });

        jToolBar1.setRollover(true);
        jToolBar1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jToolBar1KeyPressed(evt);
            }
        });

        jTableProduto.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Descrição", "Preço"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableProduto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTableProdutoKeyPressed(evt);
            }
        });
        jScrollPane.setViewportView(jTableProduto);

        jLabel.setText("Ordenar ou Pesquisar por:");

        jComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Código", "Descrição" }));
        jComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxActionPerformed(evt);
            }
        });

        jButtonPesquisa.setText("Pesquisar");
        jButtonPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPesquisaActionPerformed(evt);
            }
        });

        jTextFieldPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldPesquisaActionPerformed(evt);
            }
        });

        jSeparator1.setForeground(new java.awt.Color(51, 51, 51));
        jSeparator1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jButtonConsultar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/faa/cstads/sca/resources/incluirPequeno.png"))); // NOI18N
        jButtonConsultar.setText("Consultar");
        jButtonConsultar.setFocusable(false);
        jButtonConsultar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonConsultar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConsultarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanelLayout = new org.jdesktop.layout.GroupLayout(jPanel);
        jPanel.setLayout(jPanelLayout);
        jPanelLayout.setHorizontalGroup(
            jPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelLayout.createSequentialGroup()
                .add(111, 111, 111)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 9, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(8, 8, 8)
                .add(jPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanelLayout.createSequentialGroup()
                        .add(jButtonPesquisa)
                        .add(18, 18, 18)
                        .add(jTextFieldPesquisa, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 159, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(jButtonConsultar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(365, 365, 365))
                    .add(jPanelLayout.createSequentialGroup()
                        .add(jLabel)
                        .add(18, 18, 18)
                        .add(jComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 87, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(486, Short.MAX_VALUE))))
        );
        jPanelLayout.setVerticalGroup(
            jPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanelLayout.createSequentialGroup()
                        .add(jPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel)
                            .add(jComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButtonPesquisa)
                            .add(jTextFieldPesquisa, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jButtonConsultar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(29, 29, 29))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)))
        );

        jTableVenda.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Quantidade", "Preço", "Subtotal"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableVenda.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableVendaMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTableVenda);

        jLabel1.setText("Produtos vendidos");

        jLabelTotal.setText("Total");

        jTextFieldTotal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldTotalActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(692, Short.MAX_VALUE)
                .add(jLabelTotal)
                .add(18, 18, 18)
                .add(jTextFieldTotal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 118, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(74, 74, 74))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelTotal)
                    .add(jTextFieldTotal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        );

        jButtonCaixaVenda.setText("Caixa");
        jButtonCaixaVenda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCaixaVendaActionPerformed(evt);
            }
        });

        jButtonFechar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/faa/cstads/sca/resources/sair.png"))); // NOI18N
        jButtonFechar.setText("Fechar");
        jButtonFechar.setFocusable(false);
        jButtonFechar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonFechar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFecharActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel4Layout.createSequentialGroup()
                .add(618, 618, 618)
                .add(jButtonCaixaVenda, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 112, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 93, Short.MAX_VALUE)
                .add(jButtonFechar)
                .add(48, 48, 48))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jButtonCaixaVenda, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jButtonFechar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jButtonNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/faa/cstads/sca/resources/new.PNG"))); // NOI18N
        jButtonNovo.setText("Novo");
        jButtonNovo.setFocusable(false);
        jButtonNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNovoActionPerformed(evt);
            }
        });

        jButtonExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/faa/cstads/sca/resources/excluir2.png"))); // NOI18N
        jButtonExcluir.setText("Excluir");
        jButtonExcluir.setFocusable(false);
        jButtonExcluir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonExcluir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExcluirActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 936, Short.MAX_VALUE)
                            .add(jScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 936, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                .add(10, 10, 10)
                                .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .add(layout.createSequentialGroup()
                                .add(jToolBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(77, 77, 77)
                                .add(jPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .add(layout.createSequentialGroup()
                        .add(365, 365, 365)
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 354, Short.MAX_VALUE)
                        .add(jButtonNovo)
                        .add(18, 18, 18)
                        .add(jButtonExcluir)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jToolBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(65, 65, 65)
                .add(jScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 118, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButtonNovo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jButtonExcluir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(10, 10, 10))
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .add(18, 18, 18)))
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 155, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    private void Fechar() {
        int i;
        JTabbedPane j = (JTabbedPane) this.getParent();
        i = j.indexOfComponent(this);
        if (i != -1) {
            this.getParent().remove(i);
        }
    }

    private void configuraBotoes() {
        boolean ok = (jTableVenda.getRowCount() > 0);
        jButtonExcluir.setEnabled(ok);


    }

    private void configurarBotoes() {
        boolean ok = (jTableProduto.getRowCount() > 0);
        jButtonConsultar.setEnabled(ok);

    }

    private void atualizarTabela(List<Produto> produtos) {

        //limpa campos jtable
        DefaultTableModel dtmLimpar;
        dtmLimpar = (DefaultTableModel) jTableProduto.getModel();
        dtmLimpar.setNumRows(0);

        DefaultTableModel dtm;

        for (Produto umProduto : produtos) {
            //Carrega os valores no jTable
            dtm = (DefaultTableModel) jTableProduto.getModel();
            dtm.addRow(new Object[]{
                        umProduto.getCodigoProduto(), umProduto.getDescricao(), umProduto.getPreco()});
        }
        jTableProduto.getColumnModel().getColumn(0).setPreferredWidth(80);
        jTableProduto.getColumnModel().getColumn(1).setPreferredWidth(100);
        jTableProduto.getColumnModel().getColumn(2).setPreferredWidth(80);
        cColuna = jTableProduto.getColumnModel().getColumn(0);
        DefaultTableCellRenderer cAlign = new DefaultTableCellRenderer();
        cAlign.setHorizontalAlignment(JLabel.CENTER);
        cColuna.setCellRenderer(cAlign);
        cAlign = null;
        jTableProduto.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        if (jTableProduto.getRowCount() > 0) {
            jTableProduto.setRowSelectionInterval(0, 0);
        }
        configurarBotoes();

    }

    private void atualizarTabelaVenda(List<Venda> vendas) {


        Double total = 0.0;
        //limpa campos jtable
        DefaultTableModel dtmLimpar;
        dtmLimpar = (DefaultTableModel) jTableVenda.getModel();
        dtmLimpar.setNumRows(0);

        DefaultTableModel dtm;

        for (Venda umVenda : vendas) {
            //Carrega os valores no jTable
            dtm = (DefaultTableModel) jTableVenda.getModel();
            dtm.addRow(new Object[]{
                        umVenda.getCodigoProduto(), umVenda.getQuantidade(), umVenda.getPreco(), umVenda.getSubTotal()});

            total += umVenda.getQuantidade() * umVenda.getPreco();
        }
        jTextFieldTotal.setText(total.toString());
        jTableVenda.getColumnModel().getColumn(1).setPreferredWidth(30);
        jTableVenda.getColumnModel().getColumn(2).setPreferredWidth(20);
        jTableVenda.getColumnModel().getColumn(3).setPreferredWidth(80);
        cColuna = jTableVenda.getColumnModel().getColumn(0);
        DefaultTableCellRenderer cAlign = new DefaultTableCellRenderer();
        cAlign.setHorizontalAlignment(JLabel.CENTER);
        cColuna.setCellRenderer(cAlign);
        cAlign = null;
        jTableVenda.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        if (jTableVenda.getRowCount() > 0) {
            jTableVenda.setRowSelectionInterval(0, 0);
        }
        configuraBotoes();

    }

private void jButtonFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFecharActionPerformed
    Fechar();
}//GEN-LAST:event_jButtonFecharActionPerformed

private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
    jTableProduto.requestFocus();
}//GEN-LAST:event_formFocusGained

private void jToolBar1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jToolBar1KeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
        Fechar();
    }
}//GEN-LAST:event_jToolBar1KeyPressed

private void jTableProdutoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTableProdutoKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
        Fechar();
    }
}//GEN-LAST:event_jTableProdutoKeyPressed

private void formKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyTyped
    if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
        Fechar();
    }
}//GEN-LAST:event_formKeyTyped

private void jComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxActionPerformed
    ctrCadastroProduto.ordenar(jComboBox.getSelectedIndex(), produtos);
    atualizarTabela(produtos);
}//GEN-LAST:event_jComboBoxActionPerformed

private void jButtonPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPesquisaActionPerformed
    if (!(jTextFieldPesquisa.getText().isEmpty())) {
        boolean achou = false;
        for (int i = 0; i < jTableProduto.getModel().getRowCount(); i++) {
            if (jTextFieldPesquisa.getText().trim().toUpperCase().equals(jTableProduto.getValueAt(i, jComboBox.getSelectedIndex()).toString().trim().toUpperCase())) {
                achou = true;
                jTableProduto.setRowSelectionInterval(i, i);
                jTableProduto.repaint();
                break;
            }

        }
        jTextFieldPesquisa.setText("");
        if (!achou) {
            JOptionPane.showMessageDialog(null, "Produto não localizado", "Pesquisa", JOptionPane.OK_OPTION);
        }

    }


}//GEN-LAST:event_jButtonPesquisaActionPerformed

private void jTableVendaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableVendaMouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_jTableVendaMouseClicked

private void jTextFieldPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldPesquisaActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextFieldPesquisaActionPerformed

private void jButtonConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConsultarActionPerformed
    try {
        // TODO add your handling code here:
        JFrmProduto jDialogProduto;
        produto = new Produto();
        produto.setCodigoProduto((Integer) jTableProduto.getValueAt(jTableProduto.getSelectedRow(), 0));
        ctrCadastroProduto.carregar(produto);
        jDialogProduto = new JFrmProduto(parent, 2, produto);
        jDialogProduto.setLocationRelativeTo(this);
        jDialogProduto.setVisible(true);

    } catch (ExcecaoSCEV ex) {
        JOptionPane.showMessageDialog(null, ex.getMsg(), "Validação", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonConsultarActionPerformed

private void jButtonNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNovoActionPerformed

    JFrmVenda jDialogVenda;


    venda = new Venda();
    jDialogVenda = new JFrmVenda(parent, 0, venda);
    jDialogVenda.setLocationRelativeTo(this);
    jDialogVenda.setVisible(true);
    if (jDialogVenda.isSucesso()) {
        try {
            vendas = ctrCadastroVenda.listarTodos(jComboBox.getSelectedIndex());
            atualizarTabelaVenda(vendas);
        } catch (ExcecaoSCEV ex) {
        }
    }

}//GEN-LAST:event_jButtonNovoActionPerformed

private void jButtonExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExcluirActionPerformed
    // TODO add your handling code here:
    if (JOptionPane.showConfirmDialog(null,
            "Deseja excluir o produto " +
            jTableVenda.getValueAt(jTableVenda.getSelectedRow(), 1) +
            "?", "Vendas",
            JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
        try {
            venda = new Venda();
            venda.setCodigoProduto((Integer) jTableVenda.getValueAt(jTableVenda.getSelectedRow(), 0));
            ctrCadastroVenda.excluir(venda);
            vendas = ctrCadastroVenda.listarTodos(jComboBox.getSelectedIndex());
            atualizarTabelaVenda(vendas);
        } catch (ExcecaoSCEV ex) {
            JOptionPane.showMessageDialog(null, ex.getMsg(), "Validação", JOptionPane.ERROR_MESSAGE);
        }
    }

}//GEN-LAST:event_jButtonExcluirActionPerformed

private void jTextFieldTotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldTotalActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_jTextFieldTotalActionPerformed

private void jButtonCaixaVendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCaixaVendaActionPerformed
    // TODO add your handling code here:
    for (int i = 0; i < jTableVenda.getModel().getRowCount(); i++) {
            try {
                ProdutoDAO.alterarQuantidade((Integer)jTableVenda.getValueAt(i, 0), (Integer)jTableVenda.getValueAt(i, 1));
            } catch (ExcecaoSCEV ex) {
                Logger.getLogger(JFrmPesqVenda.class.getName()).log(Level.SEVERE, null, ex);
            }
    }

    ProprCaixaVendaJDialog proprCaixaVendaJDialog = new ProprCaixaVendaJDialog(null, true);
    proprCaixaVendaJDialog.setModo(1);
    proprCaixaVendaJDialog.setLocationRelativeTo(this);
    proprCaixaVendaJDialog.setValorTotal(Double.parseDouble(jTextFieldTotal.getText()));

    proprCaixaVendaJDialog.setVisible(true);

}//GEN-LAST:event_jButtonCaixaVendaActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JButton jButtonCaixaVenda;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonConsultar;
    private javax.swing.JButton jButtonExcluir;
    private javax.swing.JButton jButtonFechar;
    private javax.swing.JButton jButtonNovo;
    private javax.swing.JButton jButtonOk;
    private javax.swing.JButton jButtonPesquisa;
    private javax.swing.JComboBox jComboBox;
    private javax.swing.JLabel jLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelTotal;
    private javax.swing.JPanel jPanel;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTableProduto;
    private javax.swing.JTable jTableVenda;
    private javax.swing.JTextField jTextFieldPesquisa;
    public javax.swing.JTextField jTextFieldTotal;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables
}
